//
//  Score.m
//  MathMasterMain
//
//  Created by Jimmy Hahm on 10-03-29.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Score.h"


@implementation Score
@synthesize profileName;
@synthesize gameOne;
@synthesize gameTwo;
@synthesize gameThree;
@synthesize gameFour;





-(void)initScore
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"score.txt"];
	
	NSFileManager *m = [NSFileManager alloc];
	
	if([m fileExistsAtPath:appFile]){
		NSLog(@"File exists, init not required");
	} else {
		NSString *temp = @"/n/n/n/n/n/n/n/n";
		[temp writeToFile: appFile atomically: YES encoding:NSUTF8StringEncoding error:nil];
		
	}

}

-(NSMutableArray*) parseScore
{
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"score.txt"];
	NSString *info = [[NSString alloc] initWithContentsOfFile:appFile];
	NSArray *arrayOfLines = [info componentsSeparatedByString:@"\n"];
	
	NSMutableArray *_P = [NSMutableArray alloc];
	for (NSString *X in arrayOfLines)
	{
		NSArray *s = [X componentsSeparatedByString:@"/"];
		[_P addObject:s];
	}
	
	return _P;
}

-(void)addScore:(int)g
score:(int)s
name:(NSString*)n{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"score.txt"];
	NSMutableArray *x = [self parseScore];
	if(g == 1)
	{
		[[x objectAtIndex:0] addObject:n];
		[[x objectAtIndex:1] addObject:(NSString*)s];
	}
	if(g == 2)
	{
		[[x objectAtIndex:2] addObject:n];
		[[x objectAtIndex:3] addObject:(NSString*)s];
	}
	if(g == 3)
	{
		[[x objectAtIndex:4] addObject:n];
		[[x objectAtIndex:5] addObject:(NSString*)s];
	}
	if(g == 4)
	{
		[[x objectAtIndex:6] addObject:n];
		[[x objectAtIndex:7] addObject:(NSString*)s];
	}
	NSString *temp = [NSString alloc];
	NSString *tempx = [NSString alloc];
	for(NSArray *a in x)
	{
		for(NSString *b in a)
		{
			temp = [NSString stringWithFormat:@"%@%@%@",temp,@"/",a];
		}
		tempx = [NSString stringWithFormat:@"%@%@%@",tempx,temp,@"/n"];
	}
	
	[tempx writeToFile:appFile atomically: YES encoding:NSUTF8StringEncoding error:nil];
}

-(NSMutableArray*)readScore:(int)g
{

	NSMutableArray *x = [self parseScore];
	NSMutableArray *_r = [NSMutableArray alloc];
	if(g == 1)
	{
		[_r addObject:[x objectAtIndex:0]];
		[_r addObject:[x objectAtIndex:1]];
		 return _r;
	}
	if(g == 2)
	{
		[_r addObject:[x objectAtIndex:2]];
		[_r addObject:[x objectAtIndex:3]];
		return _r;
	}
	if(g == 3)
	{
		[_r addObject:[x objectAtIndex:4]];
		[_r addObject:[x objectAtIndex:5]];
		return _r;
	}
	if(g == 4)
	{
		[_r addObject:[x objectAtIndex:6]];
		[_r addObject:[x objectAtIndex:7]];
		return _r;
	}
}


@end